package com.useeinfo.oa.open.admin.wuHanCommon;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.AreaBiz;
import com.useeinfo.oa.modules.base.entity.Area;
import com.useeinfo.oa.modules.repair.biz.RepairLevelBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WarningGroupBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WarningGroupInnerUserBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningGroup;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/web/wuhan/warningGroup")
public class WarningGroupAction extends BaseAction {

    @Autowired
    private WarningGroupBiz warningGroupBiz;
    @Autowired
    private WarningGroupInnerUserBiz warningGroupInnerUserBiz;
    @Autowired
    private AreaBiz areaBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getWarningGroupListPage")
    public ModelAndView getWarningGroupListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/wuHanCommon/warningGroup/warningGroupList");

        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getWarningGroupListJSON")
    @ResponseBody
    public JSONObject getWarningGroupListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                              @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                              @RequestParam(value = "orderBy", required = false) String orderBy,
                                              @RequestParam(value = "searchCode", required = false) String searchCode) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return warningGroupBiz.findJSONList(queryParam);
    }

    /**
     * 获取编辑页面
     */
    @RequestMapping("/getWarningGroupEditPage")
    public ModelAndView getWarningGroupEditPage(@RequestParam(value = "id", required = false) String id) {


        WarningGroup warningGroup = warningGroupBiz.findModel(StringConverters.ToLong(id));
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("pages/wuHanCommon/warningGroup/warningGroupEditPart");
        modelAndView.addObject("warningGroup", warningGroup);
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeWarningGroupEdit")
    @ResponseBody
    public ResultDto executeWarningGroupEdit(WarningGroup warningGroup,
                                             @RequestParam(value = "treesData", required = false) List<String> treesData,
                                             @RequestParam(value = "select_reportArea[]", required = false) List<String> reportAreas) {
        if (CollectionUtils.isNotEmpty(reportAreas))
            warningGroup.setReportArea(areaBiz.findModel(StringConverters.ToLong(reportAreas.get(0))));
        if (warningGroupBiz.existWarningGroup(warningGroup)) {
            return ResultUtil.error("名称为空或不存在，请重新输入");
        }
        if (warningGroupBiz.existWarningGroupByCode(warningGroup)) {
            return ResultUtil.error("编码为空或不存在，请重新输入");
        }
        return warningGroupInnerUserBiz.saveWarningGroup(warningGroup, treesData);
    }

    /**
     * 逻辑删除
     */
    @RequestMapping("/logicRemove")
    @ResponseBody
    public ResultDto logicRemove(@RequestParam(value = "id", required = false) String id) {
        warningGroupBiz.deleteLogic(warningGroupBiz.findModel(StringConverters.ToLong(id)));
        return ResultUtil.success();
    }
}
